﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QQRobot
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Refresh_Click(object sender, EventArgs e)
        {
            try
            {
                pic_code.Image = Resource.load;

                Session.cookie = new CookieContainer();

                Dictionary<string, string> args = new Dictionary<string, string>
                {
                    ["appid"] = "501004106",
                    ["e"] = "2",
                    ["l"] = "M",
                    ["s"] = "3",
                    ["d"] = "72",
                    ["v"] = "4",
                    ["daid"] = "164",
                    ["t"] = new Random().NextDouble().ToString(),
                    ["pt_3rd_aid"] = "0"
                };

                Http.Get("https://web2.qq.com/", null, null, ref Session.cookie);
                byte[] datas = Http.Get("https://ssl.ptlogin2.qq.com/ptqrshow", args, null, ref Session.cookie);
                 datas = Http.Get("https://ssl.ptlogin2.qq.com/ptqrshow", args, null, ref Session.cookie);

                pic_code.Image.Dispose();
                pic_code.Image = Utils.BytesToImage(datas);

            }
            catch (Exception)
            {
                pic_code.Image = Resource.err;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                
                Dictionary<string, string> args = new Dictionary<string, string>
                {
                    ["aid"] = "501004106",
                    ["daid"] = "164",
                    ["from_ui"] = "1",
                    ["g"] = "1",
                    ["h"] = "1",
                    ["js_type"] = "1",
                    ["js_ver"] = "10284",
                    ["login_sig"] = Utils.GetCookie("pt_login_sig", Session.cookie),
                    ["mibao_css"] = "m_webqq",
                    ["t"] = "1",
                    ["pt_uistyle"] = "40",
                    ["pt_randsalt"] = "0",
                    ["ptlang"] = "2052",
                    ["ptredirect"] = "0",
                    ["ptqrtoken"] = Utils.GetHash(Utils.GetCookie("qrsig", Session.cookie)),
                    ["webqq_type"] = "10",
                    ["remember_uin"] = "1",
                    ["login2qq"] = "1",
                    ["u1"] = "https%3A%2F%2Fweb2.qq.com%2Fproxy.html"
                };
                
                byte[] datas = Http.Get("https://ssl.ptlogin2.qq.com/ptqrlogin", args,null, ref Session.cookie);

                string value = Utils.BytesToStringUTF8(datas);

                string[] splits = value.Split(',');

                if (value.Contains("ptuiCB"))
                {
                    value = value.Substring(7);
                    value = value.Substring(0, value.Length - 1);

                    if (splits[2].Contains("ptlogin"))
                    {
                        splits[2] = splits[2].Substring(1, splits[2].Length - 2);
                        Console.WriteLine(splits[2]);
                    }
                }

                Http.Get(splits[2], null, null, ref Session.cookie);

                //Console.WriteLine(Utils.GetCookie("ptwebqq", Session.cookie));

                Dictionary<string, string> vfargs = new Dictionary<string, string>
                {
                    ["clientid"] = "53999199",
                    ["psessionid"] = "",
                    ["ptwebqq"] = "",
                    ["t"] = Utils.GetTimeStamp(DateTime.Now)
                };

                Dictionary<string, string> head = new Dictionary<string, string>
                {
                    ["Referer"] = "https://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1",
                    ["Host"] = "s.web2.qq.com"  
                };

                string getvf = Utils.BytesToStringUTF8(Http.Get("https://s.web2.qq.com/api/getvfwebqq", vfargs, head, ref Session.cookie));

                Console.WriteLine(getvf);
            }
            catch (Exception e1)
            {
                throw e1;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<Cookie> cookies =  Utils.GetCookieList(Session.cookie);
            foreach(Cookie c in cookies)
            {
                Console.WriteLine("{0} {1}", c.Name, c.Value);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Console.Clear();
        }
    }
}
